/*
 * This computer program is the confidential information and proprietary trade
 * secret of Apptonelabs, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * Apptonelabs, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of Apptonelabs, Inc.
 * 
 * Copyright (c) 2012-2015 Apptonelabs, Inc. All Rights Reserved.
 */
package com.apptonelabs.vnc.vphone;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//
// The panel which implements the user authentication scheme
//

class AuthPanel extends Panel implements ActionListener {

	TextField passwordField;
	Button okButton;

	//
	// Constructor.
	//

	public AuthPanel(VPhone viewer) {
		Label titleLabel = new Label("VNC Authentication", Label.CENTER);
		titleLabel.setFont(new Font("Helvetica", Font.BOLD, 18));

		Label promptLabel = new Label("Password:", Label.CENTER);

		passwordField = new TextField(10);
		passwordField.setForeground(Color.black);
		passwordField.setBackground(Color.white);
		passwordField.setEchoChar('*');

		okButton = new Button("OK");

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();

		setLayout(gridbag);

		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.insets = new Insets(0, 0, 20, 0);
		gridbag.setConstraints(titleLabel, gbc);
		add(titleLabel);

		gbc.fill = GridBagConstraints.NONE;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gridbag.setConstraints(promptLabel, gbc);
		add(promptLabel);

		gridbag.setConstraints(passwordField, gbc);
		add(passwordField);
		passwordField.addActionListener(this);

		// gbc.ipady = 10;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 20, 0, 0);
		gbc.ipadx = 30;
		gridbag.setConstraints(okButton, gbc);
		add(okButton);
		okButton.addActionListener(this);
	}

	//
	// Move keyboard focus to the default object, that is, the password
	// text field.
	//

	public void moveFocusToDefaultField() {
		passwordField.requestFocus();
	}

	//
	// This method is called when a button is pressed or return is
	// pressed in the password text field.
	//

	public synchronized void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == passwordField || evt.getSource() == okButton) {
			passwordField.setEnabled(false);
			notify();
		}
	}

	//
	// Wait for user entering a password, and return it as String.
	//

	public synchronized String getPassword() throws Exception {
		try {
			wait();
		} catch (InterruptedException e) {
		}
		return passwordField.getText();
	}

}
